<template>
  <!-- 工单处理记录表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
      <el-col :span="12">
      <el-form-item label="版本" prop="version">
      <el-input v-if="!props.isReadonly" v-model="formData.version" placeholder="请输入版本" clearable />
  
      <span v-else>{{formData.version}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理实例主键" prop="processingInstanceId">
      <el-input v-if="!props.isReadonly" v-model="formData.processingInstanceId" placeholder="请输入处理实例主键" clearable />
  
      <span v-else>{{formData.processingInstanceId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理类别" prop="processingCategory">
      <el-input v-if="!props.isReadonly" v-model="formData.processingCategory" placeholder="请输入处理类别" clearable />
  
      <span v-else>{{formData.processingCategory}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理人主键" prop="processingUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.processingUserId" placeholder="请输入处理人主键" clearable />
  
      <span v-else>{{formData.processingUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理人姓名" prop="processingUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.processingUserName" placeholder="请输入处理人姓名" clearable />
  
      <span v-else>{{formData.processingUserName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理动作" prop="processingAction">
      <el-input v-if="!props.isReadonly" v-model="formData.processingAction" placeholder="请输入处理动作" clearable />
  
      <span v-else>{{formData.processingAction}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理动作描述" prop="processingActionDescribe">
      <el-input v-if="!props.isReadonly" v-model="formData.processingActionDescribe" placeholder="请输入处理动作描述" clearable />
  
      <span v-else>{{formData.processingActionDescribe}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理描述" prop="processingOpinion">
      <el-input v-if="!props.isReadonly" v-model="formData.processingOpinion" placeholder="请输入处理描述" clearable />
  
      <span v-else>{{formData.processingOpinion}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="处理时间" prop="processingTime">
      <el-input v-if="!props.isReadonly" v-model="formData.processingTime" placeholder="请输入处理时间" clearable />
  
      <span v-else>{{formData.processingTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="删除标志" prop="deleted">
      <el-input v-if="!props.isReadonly" v-model="formData.deleted" placeholder="请输入删除标志" clearable />
  
      <span v-else>{{formData.deleted}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建时间" prop="createTime">
      <el-input v-if="!props.isReadonly" v-model="formData.createTime" placeholder="请输入创建时间" clearable />
  
      <span v-else>{{formData.createTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建用户主键" prop="createdUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserId" placeholder="请输入创建用户主键" clearable />
  
      <span v-else>{{formData.createdUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建用户姓名" prop="createdUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserName" placeholder="请输入创建用户姓名" clearable />
  
      <span v-else>{{formData.createdUserName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="最后更新时间" prop="lastUpdateTime">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdateTime" placeholder="请输入最后更新时间" clearable />
  
      <span v-else>{{formData.lastUpdateTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="最后更新用户主键" prop="lastUpdatedUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserId" placeholder="请输入最后更新用户主键" clearable />
  
      <span v-else>{{formData.lastUpdatedUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="最后更新用户姓名" prop="lastUpdatedUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserName" placeholder="请输入最后更新用户姓名" clearable />
  
      <span v-else>{{formData.lastUpdatedUserName}}</span>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "EAMESMESMWORKORDERPROCESSINGRECORDADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    version: [
                        ],
    processingInstanceId: [
                        ],
    processingCategory: [
                        ],
    processingUserId: [
                        ],
    processingUserName: [
                        ],
    processingAction: [
                        ],
    processingActionDescribe: [
                        ],
    processingOpinion: [
                        ],
    processingTime: [
                        ],
    deleted: [
                        ],
    createTime: [
                        ],
    createdUserId: [
                        ],
    createdUserName: [
                        ],
    lastUpdateTime: [
                        ],
    lastUpdatedUserId: [
                        ],
    lastUpdatedUserName: [
                        ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
 id: "",  version: "",  processingInstanceId: "",  processingCategory: "",  processingUserId: "",  processingUserName: "",  processingAction: "",  processingActionDescribe: "",  processingOpinion: "",  processingTime: "",  deleted: "",  createTime: "",  createdUserId: "",  createdUserName: "",  lastUpdateTime: "",  lastUpdatedUserId: "",  lastUpdatedUserName: "",   });

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  version,  processingInstanceId,  processingCategory,  processingUserId,  processingUserName,  processingAction,  processingActionDescribe,  processingOpinion,  processingTime,  deleted,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  } = newVal;
              Object.assign(formData, {  id,  version,  processingInstanceId,  processingCategory,  processingUserId,  processingUserName,  processingAction,  processingActionDescribe,  processingOpinion,  processingTime,  deleted,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.version = ""
    formData.processingInstanceId = ""
    formData.processingCategory = ""
    formData.processingUserId = ""
    formData.processingUserName = ""
    formData.processingAction = ""
    formData.processingActionDescribe = ""
    formData.processingOpinion = ""
    formData.processingTime = ""
    formData.deleted = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
  }

  const loading = ref(false);

  /** 工单处理记录保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/eam/esmworkorderprocessingrecord/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
